// 初始化界面UI
$.afui.useOSThemes = false;
$.afui.autoLaunch = false;
// 地图对象
var map;
var map2;
var map3;
var infoWindow;
var infoWindow2;
var points;
var markers;
var IMEI1;
var IMEI2;

//测试：www.snnyj.cn
//正常：var host = "www.mingyitech.cn";
//吴：192.168.1.222
//http://182.254.210.168:8080/admin/index.action
//http://182.254.139.163:8080/admin/index.action
var host = "www.mingyitech.cn";
var webhost = "www.mingyitech.cn";
var socket;
var isConnected = false;
var uGetGPS_flag = false;
var uBandTerminal_flag=false;
var uBandTerminal_panel=false;
/*定时器*/
var ctime=null;
// 分页
var pageIndex = 1;
var pageSize = 10;
$(document).ready(function() {
    // 初始化socket连接 , "flashsocket", "htmlfile", "xhr-polling", "jsonp-polling"
    //重连延迟500ms 最大3000ms,次数3次
    socket = io("ws://" + host + ":8888", {
        transports : ["websocket"],
        timeout : 3000,
        reconnect : true,
        reconnectionDelay : 500,
        reconnectionDelayMax : 3000,
        reconnectionAttempts : 3,
        autoConnect : true,
    });
    socket.on("connect", function() {
        if (localStorage.getItem("loginUser") != null) {
            sendCmd({
                c : 'uApkPulse',
                id : localStorage.getItem("loginUser").split(",")[0],
                lastLogin: new Date().getTime()
            });
            if (localStorage.getItem("device") != null) {
                sendCmd({
                    c : "uGetGPS",
                    imei : localStorage.getItem("device").split(",")[0]
                });
            }
        }
        isConnected = true;
    });

    socket.on("reconnect_failed", function() {
       // alert("连接超时，请检查网络设置。");
       isConnected = false;
        socket.connect();

    });

    socket.on("command", function(data) {
              console.log("socket command:"+data.c);
        if (data.c == "smsVerify")
            smsVerifyCallback(data);
        else if (data.c == "saveMobile") {
        	saveMobileCallback(data);
        }
        else if (data.c == "login") {
            loginCallback(data);
        } else if (data.c == "logout")
            logoutCallback(data);
        else if (data.c == "register")
            registerCallback(data);
        else if (data.c == "uGetPassword")
            forgotCallback(data);
        else if (data.c == "uResetPassword")
            resetCallback(data);
        else if (data.c == "uChangePassword")
            uChangePasswordCallback(data);
        else if (data.c == "uBandTerminal")
            bandCallback(data);
        else if (data.c == "uGetGPS")
            uGetGPSCallback(data);
        else if (data.c == "trackLog")
            trackLogCallback(data);
        else if (data.c == "fencesetting")
            fencesettingCallback(data);
        else if (data.c == "uRelateNumber")
            uRelateNumberCallback(data);
        else if (data.c == "uGetNotify")
            uGetNotifyCallback(data);
        else if (data.c == "uGetNewMessage")
            uGetNewMessageCallback(data);
        else if (data.c == "onMessage")
            onMessage(data);
        else if (data.c == "classHidding")
            classHiddingCallback(data);
        else if (data.c == "alarmPowerOnOff")
            alarmPowerOnOffCallback(data);
        else if (data.c == "alarmsetting")
            alarmsettingCallback(data);
        else if (data.c == "followingSetting")
            followingSettingCallback(data);
        else if (data.c == "monitor")
            monitorCallback(data);
        else if (data.c == "uRelation")
            uRelationCallback(data);
        else if (data.c == "uReport")
            uReportCallback(data);
        else if (data.c == "alarmPowerOnOff")
            alarmPowerOnOffCallback(data);
        else if (data.c == "uSchoolRecord")
            uSchoolRecordCallback(data);
    });
    //disconnect：断开连接
    socket.on("disconnect", function() {
       isConnected = false;
    });

});
document.addEventListener("deviceready", function() {
    // 跳过启动画面
   navigator.splashscreen.hide();
    //插件初始化

    // 显示应用页面
    $.afui.launch();
    // 加载fastclick处理
    FastClick.attach(document.body);
    // 检查是否登录
    if (localStorage.getItem("loginUser") == null) {
        $.afui.loadContent("#login", false, false, "none");
    } else {
        $.afui.loadContent("#main", false, false, "none");
        // $("#main").trigger('panelbeforeload');
        $("#main").trigger('panelload');
         $("#main").trigger('panelbeforeload');

        initNav();
       /* logout();
        $.afui.loadContent("#login", false, false, "none");*/
    }
});
function sendCmd(data) {
    console.log("socket sendCmd:"+data.c);
    socket.emit("command", JSON.stringify(data));
}

function initNav() {
    var data = localStorage.getItem("loginUser").split(",");
    var logo = "http://" + webhost + "/images/uploads/" + data[1] + ".jpg";
    $("#logoImg-menu").attr("src", logo);
    $("#name-menu").html(data[2]);
    var infoData = localStorage.getItem(data[0]);
    if (infoData != null) {
        var info = infoData.split(",-,");
        $("#logoImg-menu").attr("src", info[3]);
        $("#name-menu").html(info[4]);
    }
}

// 预览
function preview(input) {
    var reader = new FileReader();
    reader.onload = function(e) {
        $("#logoImg").attr("src", e.target.result);
    }
    reader.readAsDataURL(input.files[0]);
}

$(document).ready(function() {

 //android/ios区分
      var browser = {
                     versions: function() {
                         var u = navigator.userAgent;
                         return {
                             ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
                             android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
                             iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1,
                             iPad: u.indexOf('iPad') > -1
                         };
                     }()
                 }
     if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad){
         include_css("styles/label.css");
     }
                                            
     //map locate init
     Device.createMap();
     map3.centerAndZoom(new BMap.Point(116.4135540000,39.9110130000),16);
});

 /*
 //监听android backbutton事件
document.addEventListener("deviceready", onDeviceReady, false);
 function onDeviceReady(){
    document.addEventListener("backbutton",back,false);
   }
http://www.bubuko.com/infodetail-912089.html
 document.addEventListener("backbutton",back,false)
   console.log("backbutton取消");
   }*/